package middlewares

import (
	"github.com/gin-gonic/gin"
	"goconfcenter/models"
	"goconfcenter/pkg/snowflake"
	"goconfcenter/service"
	"time"
)

func OperationLogMiddleware(opTag string) func(ctx *gin.Context) {
	return func(ctx *gin.Context) {
		userId:=ctx.GetInt64("userId")
		loginUser:=service.BackendUserServiceInstance().GetInfo(userId)
		m :=&models.OperationLog{}
		m.ID=snowflake.GenID()
		m.RequestForm="test"
		m.OperatorId=loginUser.ID
		m.OperTag=opTag
		m.CreateTime=time.Now()
		m.Method=ctx.Request.Method
		m.RequestUri=ctx.Request.RequestURI
		m.UserIpAddr=ctx.RemoteIP()
		_ = service.OperationLogServiceInstance().Insert(m)
		ctx.Next()
	}
}
